﻿
using App.Code;
using App.Entity;
using App.IServices;
using System.Web.Mvc;

namespace AppAit
{
    public class HandlerErrorAttribute : HandleErrorAttribute
    {
        private ISys_LogServices _log;
        public HandlerErrorAttribute(ISys_LogServices log)
        {
            _log = log;
        }
        public override void OnException(ExceptionContext context)
        {
            base.OnException(context);
            context.ExceptionHandled = true;
            Sys_Log logEntity = new Sys_Log();
            logEntity.ModuleName = "系统异常";
            logEntity.Type = DbLogType.Exception.ToString();
            logEntity.Account = "system";
            logEntity.NickName = "system";
            logEntity.Result = true;
            logEntity.Description = context.Exception.Message;
            _log.WriteDbLog(logEntity);

            context.HttpContext.Response.StatusCode = 200;
            context.Result = new ContentResult { Content = new AjaxResult { state = ResultType.error.ToString(), message = context.Exception.Message }.ToJson() };
        }
        private void WriteLog(ExceptionContext context)
        {
            if (context == null)
                return;
            var log = LogFactory.GetLogger(context.Controller.ToString());
            log.Error(context.Exception);
        }
    }
}